package com.wzy.leecode.linked;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: wzy
 * @Date: 2021/11/25/19:53
 * @Description:
 */
public class SwapBorderNode {
    public static void main(String[] args) {
        Node head = new Node(1);
        for (int i = 2; i < 5; i++) {
            add(head, new Node(i));
        }
        print(head);
        Node node = swap(head);
        print(node);


    }

    public static void add(Node head, Node add) {
        if (head == null)
            return;
        while (head.next != null) {
            head = head.next;
        }
        head.next = add;
    }

    public static void print(Node head) {
        if (head == null) {
            System.out.println("链表是空的");
        }
        while (head != null) {
            System.out.print(head.value + " ");
            head = head.next;
        }
    }

    public static Node swap(Node head) {
        Node dummyNode = new Node(0);
        dummyNode.next = head;
        Node temp = dummyNode;
        while (temp.next != null && temp.next.next != null) {
            Node node1 = temp.next;
            Node node2 = temp.next.next;
            temp.next = node2;
            node1.next = node2.next;
            node2.next = node1;
            temp = node1;
        }
        return dummyNode.next;
    }
}
